package cn.edu.hust.sse.software7.class3.DAO;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import cn.edu.hust.sse.software7.class3.common.PMF;
import cn.edu.hust.sse.software7.class3.entities.EBCategory;

public class EBCategoryDAO {
	private static final Logger log = Logger.getLogger(EBAdminDAO.class
			.getName());

	public boolean isExistCategory(EBCategory category) {
		log.info("check whether the category " + category.getCategoryName()
				+ " exist begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		boolean result;
		try {
			tx.begin();
			Query query = pm.newQuery(EBCategory.class);

			String filter = "categoryName == '" + category.getCategoryName()
					+ "'";
			query.setFilter(filter);
			List<EBCategory> categories = (List<EBCategory>) query.execute();
			if (categories.isEmpty()) {
				log.finest("the category " + category.getCategoryName()
						+ " isnot exist");
				result = false;
			} else {
				log.finest("the category  " + category.getCategoryName()
						+ "already exist.");
				result = true;
			}
			tx.commit();
		} catch (Exception e) {

			log.info("check category exist occur exception!!!\n"
					+ e.getStackTrace());
			result = true;
		}
		return result;
	}

	public void addCategory(EBCategory category) {
		log.info("add category " + category.getCategoryName() + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.makePersistent(category);
			tx.commit();
			log.finest("add category " + category.getCategoryName()
					+ " success.");
		} catch (Exception e) {
			log.info("add category " + category.getCategoryName()
					+ "failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public void deleteCategory(EBCategory category) {
		log.info("delete category " + category.getCategoryName() + " begin.");
		String delName = category.getCategoryName();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.deletePersistent(pm.getObjectById(EBCategory.class,
					category.getCategoryId()));
			tx.commit();
			log.finest("delete category " + delName + " success.");
		} catch (Exception e) {
			log.info("delete category " + delName + "failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public void deleteAllCategory(List<EBCategory> categories) {
		log.info("delete categories " + categories + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.deletePersistentAll(categories);
			tx.commit();
			log.finest("delete categories  success.");
		} catch (Exception e) {
			log.info("delete categories failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public List<EBCategory> getCategories() {
		log.info("get all the categories begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBCategory> categories = new ArrayList<EBCategory>();
		try {
			tx.begin();
			Query query = pm.newQuery(EBCategory.class);
			List<EBCategory> cs = (List<EBCategory>) query.execute();

			if (cs.isEmpty()) {
				log.finest("there is no categories");
			} else {
				categories = cs;
				log.finest(categories.size() + " categories in total.");
			}
			tx.commit();
		} catch (Exception e) {
			log.info("get all the categories occur exception!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return categories;
	}

	public EBCategory getCategoryById(Long categoryId) {
		log.info("get EBCategory with id " + categoryId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		EBCategory category = null;
		try {
			tx.begin();
			category = pm.getObjectById(EBCategory.class, categoryId);
			tx.commit();
			log.finest("get EBCategory with id " + categoryId + " success.");
		} catch (Exception e) {
			log.info("get EBCategory with id " + categoryId + "failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return category;
	}

	public List<EBCategory> getCategoriesByPid(Long parentCId) {
		log.info("get categories by parent category id");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBCategory> categories = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBCategory.class);
			query.setFilter("superCategoryId == " + parentCId);
			categories = (List<EBCategory>) query.execute();
			tx.commit();
			log.info("get " + categories.size() + " categories with parent "
					+ parentCId);
		} catch (Exception e) {
			log.info("get " + categories.size() + " categories with parent "
					+ parentCId);
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return categories;
	}

	public void deleteCategoryById(Long categoryId) {
		log.info("delete category by  category id " + categoryId);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.deletePersistent(pm.getObjectById(EBCategory.class, categoryId));
			tx.commit();
			log.info("deleted category by  category id " + categoryId);
		} catch (Exception e) {
			log.info("delete category by  category id " + categoryId
					+ " occurred errors");
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}
}
